/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.web30.mobile.view.news;

import com.sun.syndication.feed.synd.SyndEntry;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author xiazy
 */
public class Reader {

    private String org = "www.web30core.org";
    private Feed _feed = null;
    private Entry _currentEntry = null;

    public Entry getCurrentEntry() {
        return _currentEntry;
    }

    public void setCurrentEntry(Entry _currentEntry) {
        this._currentEntry = _currentEntry;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public Feed getFeed() {
        return _feed;
    }

    public void setFeed(Feed feed) {
        this._feed = feed;
    }

    public Entry getEntry(int index) {
        if(this._feed == null){
            this.init();
        }
        return (Entry) this._feed.getEntries().get(index);
    }

    public String onSelectEntry() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        int index = Integer.parseInt((String) ctx.getExternalContext().getRequestParameterMap().get("index"));
        this._currentEntry = this.getEntry(index);
        return "entry";
    }

    public String onPreviousEntry() {
        int index = this._currentEntry.getIndex();
        if (index > 0) {
            index--;
            this._currentEntry = this.getEntry(index);
        }
        return "entry";
    }

    public String onNextEntry() {
        int index = this._currentEntry.getIndex();
        if (index < this._feed.getEntries().size() - 1) {
            index++;
            this._currentEntry = this.getEntry(index);
        }
        return "entry";
    }

    public String onReturn() {
        return "return";
    }

    public String onRefresh() {
        this.init();
        return "refresh";
    }
    public Reader() {
        this.init();
    }

    private void init() {
        try {
            this._feed = new Feed(
                    new URL("http://www.web30core.org:46120/NNTPBridge/resources/news/trash/daily"));
        } catch (MalformedURLException ex) {
            Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
